package com.base.cn.platform.os.controller.statistics.base;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.base.ZStatisticsUserBaseCount;
import com.base.cn.platform.os.service.statistics.base.ZStatisticsUserBaseCountBiz;
import com.base.cn.platform.os.service.user.user.CusUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 基础统计中用户注册数据统计Controller
 *
 * @author s.li
 * @date 2019/04/18
 */
@RestController
@RequestMapping("/manage/statistics")
public class ZStatisticsUserBaseCountController extends BaseController {

    private DecimalFormat decimalFormat = new DecimalFormat("#.##");
    @Autowired
    private ZStatisticsUserBaseCountBiz zStatisticsUserBaseCountBiz;
    @Autowired
    private CusUserBiz cusUserBiz;

    /**
     *  获取用户统计数据
     * @return Map<String,Object>
     */
    @RequestMapping("/queryUserBaseCountList")
    public Map<String,Object> queryUserBaseCountList(@RequestBody StatisticsDateQueryCondition condition ){
        //总用户量
        BigDecimal sumUserCount = cusUserBiz.count(" 1=1");

        //设置时间条件
        Date nowDate =new Date();
        condition.setTimeCondition(nowDate);
        List<ZStatisticsUserBaseCount> userBaseCountList = zStatisticsUserBaseCountBiz.queryUserBaseCountList(condition);
        //注册人数
        int regCount;
        //注册率
        Float regRate;
        //活跃率
        Float activityRate;
        //访问UV
        int visitorCount;
        //活跃用户
        int activityUserCount;

        if(condition.getType()==1){//当天的
            //注册总人数
            regCount = userBaseCountList.stream().mapToInt(ZStatisticsUserBaseCount::getRegCount).sum();
            //访问UV
            visitorCount = userBaseCountList.get(0).getVisitorCount();
            //活跃用户
            activityUserCount = userBaseCountList.get(0).getActivityUserCount();
        }else{
            //注册总人数
            regCount = userBaseCountList.stream().mapToInt(ZStatisticsUserBaseCount::getRegCount).sum();
            //访问UV
            visitorCount = userBaseCountList.stream().mapToInt(ZStatisticsUserBaseCount::getVisitorCount).sum();
            //活跃用户
            activityUserCount = userBaseCountList.stream().mapToInt(ZStatisticsUserBaseCount::getActivityUserCount).sum();
        }
        //注册率
        regRate  = new Float(regCount) / new Float(visitorCount);
        if(regRate.isNaN() || regRate.isInfinite()){
            regRate = 0f;
        }

        //活跃率
        activityRate = new Float(activityUserCount) / new Float(sumUserCount.intValue());
        if(activityRate.isInfinite() || activityRate.isNaN()){
            activityRate = 0f;
        }
        regRate = regRate * 100f;
        activityRate = activityRate * 100f;
        Map<String,Object> resultMap = ResultUtil.SUCCESS(userBaseCountList);
        resultMap.put("regCount",regCount);
        resultMap.put("regRate",decimalFormat.format(regRate));
        resultMap.put("activityRate",decimalFormat.format(activityRate));
        resultMap.put("type",condition.getType());
        return resultMap;
    }
}
